﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using SAM_Edu.entity;

namespace SAM_Edu.dao
{
    public class daoLicenseKey
    {

        static MySqlConnection conn;

        static bool status = false;
        public static bool StoreLicenseKey(LicenseKey lk)
        {

            conn = DatabaseConnection.getConnection();
            string Query = "INSERT INTO licenseKey(Licensekey, activationStatus, activationDTS, lastValidationDTS, lastValidationStatus, numbOfWarning) VALUES('" + lk.Licensekey + "','" + lk.ActivationStatus + "','" + lk.ActivationDTS.ToString("yyyy-MM-dd HH:mm:ss") + "','" + lk.LastValidationDTS.ToString("yyyy-MM-dd HH:mm:ss") + "','" + lk.LastValidationStatus + "','" + lk.NumbOfWarning + "')";
            MySqlCommand cmd = new MySqlCommand(Query, conn);

            conn.Open();
            int result = cmd.ExecuteNonQuery();

            if (result > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
            conn.Close();
            return status;
        }
        public static string CheckLicense()
        {
            MySqlConnection conn = DatabaseConnection.getConnection();
            string Query = "Select * FROM licensekey";
            MySqlCommand MyCommand2 = new MySqlCommand(Query, conn);
            MySqlDataReader MyReader2;
            conn.Open();
            MyReader2 = MyCommand2.ExecuteReader();
            while (MyReader2.Read())
            {
                return MyReader2[0].ToString();
                break;
            }
            conn.Close();
            return "";
        }

        public static DateTime ValidateLicense()
        {
            MySqlConnection conn = DatabaseConnection.getConnection();
            string Query = "Select * FROM licensekey";
            MySqlCommand MyCommand2 = new MySqlCommand(Query, conn);
            MySqlDataReader MyReader2;
            conn.Open();
            MyReader2 = MyCommand2.ExecuteReader();
            MyReader2.Read();

            return DateTime.Parse(MyReader2[3].ToString());
            conn.Close();



        }
        public static bool updateLastActivationDate(string licensekey)
        {
            conn = DatabaseConnection.getConnection();
            string Query = "UPDATE licensekey SET lastValidationDTS='"+ System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "'WHERE licensekey='" + licensekey + "'";
            MySqlCommand cmd = new MySqlCommand(Query, conn);

            conn.Open();
            int result = cmd.ExecuteNonQuery();

            if (result > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
            conn.Close();
            return status;

        }

    }
}
